package com.hu.zhcc.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.controller.Appointment.ReserController;
import com.hu.zhcc.shiro.entity.bo.MedicalExaminationReport;
import com.hu.zhcc.shiro.entity.bo.Member;
import com.hu.zhcc.shiro.service.Appointment.ReserService;
import com.hu.zhcc.shiro.service.MERService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/report")
public class MedicalExaminationReportController {
    @Autowired
    MERService merService;
    @Autowired
    ReserService reserService;
    @GetMapping("listAllReport")
    public ResponseEntity<PageInfo<MedicalExaminationReport>> listAllReport(@RequestParam(required = false) String name,
                                                          @RequestParam(required = false) String sort, int offset, int limit) {

        PageInfo<MedicalExaminationReport> list = merService.getMedicalExaminationReportByName(offset, limit,name);

        return ResponseEntity.ok(list);
    }
    @GetMapping("getReport")
    public ResponseEntity<MedicalExaminationReport> getReport(Integer id) {
        MedicalExaminationReport model = merService.getMedicalExaminationReport(id);


        return ResponseEntity.ok(model);
    }
    @PostMapping("updateReport")
    public ResponseEntity<Integer> updateReport(@RequestBody MedicalExaminationReport medicalExaminationReport) {
        int i = merService.updateMedicalExaminationReport(medicalExaminationReport);



        return ResponseEntity.ok(i);
    }

    @PostMapping("addReport")
    public ResponseEntity<Integer> addReport(@RequestBody MedicalExaminationReport medicalExaminationReport) {
        int i = merService.addMedicalExaminationReport(medicalExaminationReport);



        return ResponseEntity.ok(i);
    }
    @PostMapping("deleteReport")
    public ResponseEntity<Integer> deleteReport(Integer id) {
        int i = merService.deleteMedicalExaminationReport(id);


        return ResponseEntity.ok(i);
    }
    @PostMapping("fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest req)
            throws IllegalStateException, IOException {

        // 判断文件是否为空，空则返回失败页面
        if (file.isEmpty()) {
            return "failed";
        }
        // 获取文件存储路径（绝对路径）
        String path = "D:\\健康管理项目图片库";
        // 获取原文件名
        String fileName = file.getOriginalFilename();
        // 创建文件实例
        File filePath = new File(path, fileName);
        // 如果文件目录不存在，创建目录
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录" + filePath);
        }
        // 写入文件
        file.transferTo(filePath);
        return path+"\\"+fileName;
    }
}
